package com.colortea.action.frontstage.membercenter;

import java.util.Date;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import com.colortea.action.backstage.base.DataCollectionTAction;
import com.colortea.action.backstage.utils.SHA1;
import com.colortea.action.backstage.utils.statickey.StaticKey;
import com.colortea.entity.MemberT;
import com.colortea.service.MemberTService;
import com.colortea.service.impl.Serial;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 *	注册相关的action
 * @author glory
 *	@data 2015年9月18日
 */
@ParentPackage("colortea")
@Namespace("")
@InterceptorRefs({@InterceptorRef("defaultStack") })
public class RegisterAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1031544679007605821L;
	@Resource
	private MemberTService memberTService;
	@Resource
	private DataCollectionTAction dataCollectionTAction;
	@Resource
	private Serial serial;
	private MemberT memberT;
	private String loginname;
	private String loginpwd;
	private String email;
	private String rand;
	private String mid;
	private String question;
	private String answer;
	private String nick;
	private String msg;
	private boolean sucflag;


	@JSON(serialize = false)
	public MemberTService getMemberTService() {
		return memberTService;
	}

	public void setMemberTService(MemberTService memberTService) {
		this.memberTService = memberTService;
	}


	public String getRand() {
		return rand;
	}

	public void setRand(String rand) {
		this.rand = rand;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public boolean isSucflag() {
		return sucflag;
	}

	public void setSucflag(boolean sucflag) {
		this.sucflag = sucflag;
	}

	public String getQuestion() {
		return question;
	}

	public void setQuestion(String questoin) {
		this.question = questoin;
	}

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public MemberT getMemberT() {
		return memberT;
	}

	public void setMemberT(MemberT memberT) {
		this.memberT = memberT;
	}

	public String getLoginname() {
		return loginname;
	}

	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}

	public String getLoginpwd() {
		return loginpwd;
	}

	public void setLoginpwd(String loginpwd) {
		this.loginpwd = loginpwd;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getNick() {
		return nick;
	}

	public void setNick(String nick) {
		this.nick = nick;
	}

	public String getMid() {
		return mid;
	}

	public void setMid(String mid) {
		this.mid = mid;
	}

	/**
	 * 清理错误
	 */
	@Override
	public void validate() {
		this.clearErrorsAndMessages();

	}

	/**
	 * 注册验证
	 * 
	 * @return
	 */
	public boolean registervalidation() {
		boolean register = false;
		this.setMsg("");
		if (StringUtils.isNotBlank(this.getLoginname())) {
			if (this.getLoginname().length() < 4) {
				this.setMsg("1");
				return register;
			} else if (this.getLoginname().length() > 40) {
				this.setMsg("1");
				return register;
			}
		} else {
			this.setMsg("1");
			return register;
		}
		if (StringUtils.isNotBlank(this.getLoginpwd())) {
			if (this.getLoginpwd().length() < 6) {
				this.setMsg("2");
				return register;
			} else if (this.getLoginpwd().length() > 20) {
				this.setMsg("2");
				return register;
			}
		} else {
			this.setMsg("2");
			return register;
		}
		register = true;
		return register;

	}

	/**
	 * 增加新用户
	 * 
	 * @return
	 */
	@Action(value = "register", results = { @Result(name = "register_success", type = "redirect", location = "/index.action"), @Result(name = "register_error", type = "redirect", location = "/doRegister.action?msg=${msg}") })
	public String register() {
		this.dataCollectionTAction.putBaseInfoToContext();
		if (registervalidation()) {
			Criterion criterion = Restrictions.and(Restrictions.eq("loginname", loginname));
			MemberT memberT = memberTService.findOneByCriteria(MemberT.class, criterion);
			if (memberT == null) {
				memberT =new MemberT();
				memberT.setId(serial.Serialid(Serial.MEMBER));
				memberT.setLoginname(loginname);
				memberT.setNick(loginname);
				memberT.setRealname(StaticKey.EMPTY);
				memberT.setCity(StaticKey.EMPTY);
				memberT.setDistrict(StaticKey.EMPTY);
				memberT.setSex(StaticKey.EMPTY);
				memberT.setWhichsex(StaticKey.EMPTY);
				memberT.setMerrystatus(StaticKey.EMPTY);
				memberT.setBirthday(StaticKey.EMPTY);
				memberT.setBlood(StaticKey.EMPTY);
				memberT.setConstellation(StaticKey.EMPTY);
				memberT.setDes(StaticKey.EMPTY);
				memberT.setEmail(StaticKey.EMPTY);
				memberT.setQq(StaticKey.EMPTY);
				memberT.setWeixin(StaticKey.EMPTY);
				memberT.setSinaweibo(StaticKey.EMPTY);
				memberT.setTag(StaticKey.EMPTY);
				memberT.setBelove(0);
				memberT.setLoveother(0);
				memberT.setPostingcount(0);
				memberT.setQuestion(StaticKey.EMPTY);
				memberT.setAnswer(StaticKey.EMPTY);
				memberT.setHeadpath(StaticKey.EMPTY);
				memberT.setMobile(StaticKey.EMPTY);
				memberT.setVersiont(0);
				memberT.setMid(StaticKey.EMPTY);
				memberT.setPaypassword(StaticKey.EMPTY);
				memberT.setMemberGroupId(StaticKey.EMPTY);
				memberT.setMemberGroupName(StaticKey.EMPTY);
				memberT.setIntegration(0);
				memberT.setTelno(StaticKey.EMPTY);
				memberT.setLoginpwd(SHA1.getDigestOfString(loginpwd));
				memberT.setCreatetime(new Date());
				memberT.setUpdatetime(new Date());
				// 自动激活账户
				memberT.setMemberstate(StaticKey.MEMBERSTATE_ONE_NUM);
				memberTService.save(memberT);
				ActionContext.getContext().getSession().put(StaticKey.MEMBER_SESSION_KEY, memberT);
				return "register_success";
			}
			this.setMsg("3");
			ActionContext.getContext().getSession().put("loginnamestatus", 0);
		}
		return "register_error";
	}
	/**
	 * 进入注册页面
	 * 
	 * @return
	 */
	@Action(value = "doRegister", results = { @Result(name = "success", type = "freemarker", location = "/WEB-INF/theme/default/shop/register.ftl"), @Result(name = "input", type = "redirect", location = "/index.html") })
	public String doRegister() {
		this.dataCollectionTAction.putBaseInfoToContext();
		return SUCCESS;
	}

	

}
